\section{Program}
\label{sec:prog}
To explore I/O in C++ and in particular formatting integers I have created a small program.
It consists of three classes (\texttt{IntegerFormatter}, \texttt{OutFormatter}, and \texttt{InFormatter}) and a driver.
The driver is simply the entry point to the program, i.e. the \texttt{main} function.
To print an integer with a width of 10 in hexadecimal, \texttt{IntegerFormatter} can be used as follows:
\begin{lstlisting}[style=code, float=false]
cout << IntegerFormatter()
    .setWidth(10)
    .setPadding('0')
    .setBase(16)
    .setShowBase()
    .out(42) << endl; // Print 000002A_16
\end{lstlisting}
The difference between this and the chaining with the $<<$ operator and stream manipulators (seen in \subref{sub:streamManip}) is that here we do not change the options of the stream (\texttt{cout} in both code snippets).
Another subtle difference is that I have chosen to postfix the base (...\_16) instead of prefixing (0x...).
The reason for this is that I have implemented more bases than just 8, 10, and 16.

Other than creating an \texttt{IntegerFormatter} for each output operation, it is possible to keep one and simply call the member function \texttt{out} for every output operation.
Below is a function which shows how an integer is printed using the regular method for printing integers and using an \texttt{IntegerFormatter}.
\begin{lstlisting}[style=code, float=false]
void print(IntegerFormatter & form, int i)
{
    ostringstream stream;
    stream << "form.out("<<i<<"): ";
    cout << setw(25) << stream.str() << form.out(i) << endl;
}
\end{lstlisting}

\subsection{IntegerFormatter}
The interface to \texttt{IntegerFormatter} contains a lot of setter functions, which are used to define the formatting of integers.
The member function \texttt{out} produces an object of the class \texttt{OutFormatter}.
The interface of \texttt{IntegerFormatter} is shown in \coderef{src:IntegerFormatter}.
\begin{lstlisting}[style=code, float=false, label=src:IntegerFormatter, caption={Interface of \texttt{IntegerFormatter}}]
class IntegerFormatter
{ // ...
public:
    IntegerFormatter & setWidth(int base);
    IntegerFormatter & setPadding(char padding);
    IntegerFormatter & setUpperCase(bool upper = true);
    IntegerFormatter & setLowerCase(bool lower = true);
    IntegerFormatter & setShowBase(bool show = true);
    IntegerFormatter & setNoShowBase(bool noShow = true);
    IntegerFormatter & setIgnoreCase(bool ignore = true);
    IntegerFormatter & setAdjust(std::ios_base::fmtflags flag);
    IntegerFormatter & adjustLeft();
    IntegerFormatter & adjustRight();
    IntegerFormatter & adjustInternal();

    //Choose base of integer (2-36)
    IntegerFormatter & setBase(int base);

    OutFormatter out(int value) const;
    InFormatter in(int & value) const;

    char digitValue(int digit) const;
    int valueDigit(char digit) const;
};
\end{lstlisting}

\subsection{OutFormatter}
An object of the type \texttt{OutFormatter} holds an integer value and a reference to an \texttt{IntegerFormatter}.
The $<<$ operator is defined for \texttt{OutFormatter}, which formats the value which the \texttt{OutFormatter} holds with formatting options of the \texttt{IntegerFormatter} which is reference by the \texttt{OutFormatter}.
The actual options of the \texttt{ostream} on the left side are not changed when using the $<<$ operator with an \texttt{OutFormatter}.
This is accomplished by having and intermediate \texttt{ostringstream}~\cite[p.~640-641]{stroustrup2000}.
The options are set on that stream before the integer is formatted and put into it.
The $<<$ operator is declared as:
\begin{lstlisting}[style=code, float=false, label=src:putToOp, caption={Put to operator with \texttt{OutFormatter}}]
std::ostream & operator<<(std::ostream & stream, OutFormatter const & formatter)
\end{lstlisting}
Although the options of the input \texttt{ostream} (named \texttt{stream}) are not changed it cannot be declared as \texttt{const}, because output operations are performed on it -- output operations are not declared as \texttt{const}.
The object being output to the stream, on the other hand, should not change, hence it is declare as \texttt{const}.
The $<<$ operator must be able to access the options specified in the \texttt{IntegerFormatter}.
Had this been actual production code I would have made getter functions (probably inlined them for efficiency), but to include another C++ concept I decided to make the operator in \coderef{src:putToOp} a friend of \texttt{IntegerFormatter}.

The implementation of the $<<$ operator in \coderef{src:putToOp} allows to choose any base between 2 and 36 with which integers should be formatted.
I chose to make this because I wondered why only the bases 8, 10, and 16 where provided in the standard library.
I must admit that my implementation is not very efficient, but it is general.
Possible optimizations could be to take advantage when the base is a power of two and use streams regular base formatting when base 8, 10, or 16 is chosen.
The reason for choosing base 36 as max is simply that there are 26 letters (in the English alphabet) + 10 digits (0-9).
Which means base 36 has the digits 0,1,...,9,A,B,...,Z.
Indeed by defining a meaning value of more characters a high base could easily be chosen.

\subsection{InFormatter}
The member function \texttt{in} of \texttt{IntegerFormatter}, seen in \coderef{src:IntegerFormatter}, takes a reference to an integer and produces an \texttt{InFormatter}.
The reference is saved in the \texttt{InFormatter}.
A reference is used because we are interrested in reading from a stream and assign a new value to an integer.
Similar to the $<<$ operator for \texttt{OutFormatter}, the $>>$ operator is declared for \texttt{InFormatter} as:
\begin{lstlisting}[style=code, float=false, label=src:getFromOp, caption={Get from operator with \texttt{InFormatter}}]
std::istream & operator>>(std::istream & stream, InFormatter & formatter)
\end{lstlisting}
Since the value of the referenced integer is supposed to change, the \texttt{InFormatter} is not \texttt{const}.
A usage of \texttt{InFormatter} where input from the keyboard is to be read as binary:
\begin{lstlisting}[style=code, float=false]
int i;
cin >> IntegerFormatter().setBase(2).in(i);
cout << i << endl; // Print value of i (in decimal)
\end{lstlisting}
Input from the keyboard is read and converted to an integer and written out using regular operators, hence decimal per default.

\subsection{Evaluation}
By using the formatters presented here no options for streams need to be specified, but can be set if there is a standard which the programmer wishes to use.
For instance we might, in general, want to print integers in hexadecimal with a fixed length of 20, but some times we want to print them as binary.
This can be achieved by defining a global accessible \texttt{IntegerFormatter}:
\begin{lstlisting}[style=code, float=false]
IntegerFormatter globalForm()
{
    static IntegerFormatter form = IntegerFormatter()
        .setBase(16).setWidth(20).setPadding('0');
    return form;
}
void dummy()
{ // ...
    IntegerFormatter local = IntegerFormatter().setBase(2);
    cout << "Global out: " << globalForm().out(42) << endl;
    cout << "Local out: " << local.out(42) << endl;
}
\end{lstlisting}
It is a bit tedious to call \texttt{globalForm().out($x$)} every time some integer $x$ is to be put into a stream.
However, it is nice to be able to use the global \texttt{IntegerFormatter} both for output and input.
In C\#, instead of overloading an operator to change the way an existing type is formatted, I would either derive from the \texttt{StreamWriter} or use an extension method to overload the \texttt{WriteLine}, e.g:
\begin{lstlisting}[style=code, float=false]
public static void WriteLine(this StreamWriter stream, int i, int printBase, int width, char padder = '0')
{
		stream.WriteLine(Convert.ToString(i, printBase).PadLeft(width, padder));
}
\end{lstlisting}
The downside to this is that the arguments are passed through a single method call instead of being chained.
This might not matter much if there are only a few arguments, but once more arguments are added it is much easier to handle an object which you can simply modify as you wish instead of having all arguments ready at format/output time.
Consider wanting to format an integer as default for the application, but with a single change, say the base.
With the global \texttt{IntegerFormatter} it can simply be copied and a new base chosen:
\begin{lstlisting}[style=code, float=false]
cout << "Global altered out: " << globalForm().setBase(20).out(42) << endl;
\end{lstlisting}
In C\# all the default arguments would have to be gathered prior to the call to \texttt{WriteLine}.